package cn.tedu.online.day04;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class BlockingTest {
    public static void main(String[] args) {


        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();

        Thread producer = new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                        int m = (int) (Math.random() * 1000);
                        queue.put(m);
                        System.out.println(queue);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        };

        Runnable task = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        if (queue.size() >= 10) {
                            System.out.println(Thread.currentThread().getName() + ":" + queue.take());
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        };

        Thread consumer = new Thread(task, "线程1");
        Thread consumer1 = new Thread(task, "线程2");

        producer.start();
        consumer.start();
        consumer1.start();

    }

}
